package com.cmwa.scbp.sys.permission.operRole.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.cmwa.scbp.sys.menu.dao.MenuDao;
import com.cmwa.scbp.sys.menu.entity.MenuVo;
import com.cmwa.scbp.sys.permission.operRole.dao.OperRoleDao;
import com.cmwa.scbp.sys.permission.operRole.dao.OperRoleMenuDao;
import com.cmwa.scbp.sys.permission.operRole.entity.OperRoleMenu;
import com.cmwa.scbp.sys.permission.operRole.entity.OperRoleVo;
import com.cmwa.scbp.sys.permission.operRole.service.OperRoleManageService;
import com.cmwa.scbp.utils.SessionUtils;

@Service
public class OperRoleManageServiceImpl implements OperRoleManageService {

	@Resource
	private OperRoleDao operRoleDao;
	
	@Resource
	private OperRoleMenuDao operRoleMenuDao;
	
	@Resource
	private MenuDao menuDao;
	
	@Override
	public List<OperRoleVo> roleList() {
		return operRoleDao.roleList(null);
	}

	@Override
	public List<String> queryRoleDefaultSelectedMenu(String roleId) {
		return operRoleMenuDao.queryRoleDefaultSelectedMenu(roleId);
	}
	
	@Override
	public List<MenuVo> queryRoleDefaultSelectedButtonMenu(String roleId, String mType, String belongSystem) {
		return menuDao.queryRoleDefaultSelectedButtonMenu(roleId, mType, belongSystem);
	}
	
	
	@Override
	public void saveRoleMenu(String roleId, String menuIds, String belongSystem) throws Exception{
		String[] mIds = menuIds.split(",");
		// 登录人ID
		String employeeId = SessionUtils.getEmployee().getID();
		List<OperRoleMenu> list = new ArrayList<OperRoleMenu>();
		for (int i = 0; i < mIds.length; i++) {
			String mid = mIds[i];
			if (StringUtils.isNotBlank(mid)) {
				OperRoleMenu roleMenu = new OperRoleMenu();
				roleMenu.setRoleId(roleId);
				roleMenu.setMenuId(mid);
				roleMenu.setStatus("C");
				roleMenu.setCreateId(employeeId);
				list.add(roleMenu);
			}
		}
		// 先删除stationId对应关联表中的记录，再更新
		operRoleMenuDao.deleteRoleMenuByUnionId(roleId, belongSystem);
		// 批量保存角色和菜单的关联关系
		if (!list.isEmpty()) {
			operRoleMenuDao.insertBatchRoleMenu(list);
		}
		
	}
	
	@Override
	public String queryRoleDefaultSelectedMenuWithComma(String srcId,
			String destId, Boolean merge) {
		// 复制的源头角色
		List<String> menuList = operRoleMenuDao.queryRoleDefaultSelectedMenu(srcId);
		// 粘贴到的目的角色
		if (merge && (destId != null && (!"".equals(destId)))) {
			List<String> dmenuList = operRoleMenuDao.queryRoleDefaultSelectedMenu(destId);
			menuList.addAll(dmenuList);
		}
		// 去重
		Set<String> memuSet = asSet(menuList);
		String result = "";
		for (String val : memuSet) {
			result += val + ",";
		}
		return result;
	}
	
	/**
	 * list 转换为 set
	 * 
	 * @param list
	 * @return
	 */
	private Set<String> asSet(List<String> list) {
		Set<String> set = new HashSet<String>();
		if (list != null) {
			for (String value : list) {
				set.add(value);
			}
		}
		return set;
	}


	

}
